﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.DBModule;
using System.Text;
using System.Data;

namespace ZX.WebApp.WorkFlow
{
    public partial class WorkFlowAccredAdd : ZX.Web.UI.Controls.BasePage
    {
        public string userId = string.Empty; //用户ID
        public static int satues = 0;
          //调用流程服务接口实例
        CrmWorkFlowService.WorkFlowServiceClient workFlowServiceClient = null;
        public DataTable table = new DataTable();//查询所有流程类型
        public DataTable tables = new DataTable();//查询所有流程
        /// <summary>
        /// 待拼接的流程树
        /// </summary>
        public string workFlowTree { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();  //实例化流程服务接口 
                //this.txtAccreditToName.Text = "陈伟";
                this.txtAccreditToName.Text = this.User.UName;
               // userId = "chenwei";
                userId = this.Page.User.Identity.Name;
                Bind();
            }
        }
        public void Bind()
        {
            createTree();
        }
        private void createTree()
        {
            StringBuilder sbChilds = new StringBuilder();
            StringBuilder _strHtml = new StringBuilder();
          
           
            table = workFlowServiceClient.GetWorkFlowClass(); //查询所有流程类型
            tables = workFlowServiceClient.GetWorkFlowList(); //查询所有流程
            List<string> rows = new List<string>();
            int counts = table.Select("FatherId = '1'").Count();
            int count = counts / 2;
            int i = 0;
            if (table != null)
            {
                foreach (DataRow tree in table.Select("FatherId = '1'"))
                {
                    i++;
                    if (i == 1)
                    {
                        _strHtml.Append("<div  id='AccreditTree0'   class='whitebg ygtv-checkbox'>");
                        _strHtml.Append("<ul >");
                    }
                    else if (i == count + 1)
                    {
                        _strHtml.Append("<div  id='AccreditTree1'   class='whitebg ygtv-checkbox'>");
                        _strHtml.Append("<ul >");
                    }
                    if (rows.Count == 0)
                    {
                        rows.Add(tree["WFClassId"].ToString());
                        _strHtml.Append("<li>");
                        _strHtml.Append("<span><a href=\"#" + tree["WFClassId"].ToString() + "\" style='display:block;cursor:hand'>" + tree["Caption"].ToString() + "</a></span>");
                       //功能明细
                       AddNode(table, tree["WFClassId"].ToString(), _strHtml);
                       AddNodes(tables, tree["WFClassId"].ToString(), _strHtml);
                       _strHtml.Append("</li>");
                    }
                    else 
                    {
                        if (!rows.Contains(tree["WFClassId"].ToString()))
                        {

                            rows.Add(tree["WFClassId"].ToString());
                            if (i <= count)
                            {
                                _strHtml.Append("<li class = 'mycode'>");
                            }
                            else if (i > count)
                            {
                                _strHtml.Append("<li>");
                            }
                            _strHtml.Append("<span><a href=\"#" + tree["WFClassId"].ToString() + "\" style='display:block;cursor:hand'>" + tree["Caption"].ToString() + "</a></span>");
                            //功能明细
                            AddNode(table, tree["WFClassId"].ToString(), _strHtml);
                            AddNodes(tables, tree["WFClassId"].ToString(), _strHtml);
                            _strHtml.Append("</li>");
                        }
                    }
                    if (i == count)
                    {
                        _strHtml.Append("</ul>");
                        _strHtml.Append("</div>");
                    }
                    else if (i == counts)
                    {
                        _strHtml.Append("</ul>");
                        _strHtml.Append("</div>");
                    }
                 }
            }
            else
            {
                _strHtml.Append("<li>权限不足或数据不存在</li>");
            }
         
            
           lteAccredit.Text = _strHtml.ToString();
           
        }
        private void AddNode(DataTable tabes, string FatherId, StringBuilder sb)
        {
            if (table.Select("FatherId = '" + FatherId + "'").Count() > 0)
            {
                sb.Append("<ul>");
                foreach (DataRow v in tabes.Select("FatherId = '" + FatherId + "'"))
                {
                    sb.Append("<li>");
                    sb.Append("<span><a href=\"#" + v["WFClassId"].ToString() + "\" style='display:block;cursor:hand'>" + v["Caption"].ToString() + "</a></span>");
                    AddNode(tabes, v["WFClassId"].ToString(), sb);
                    AddNodes(tables, v["WFClassId"].ToString(), sb);
                    sb.Append("</li>");

                }
                sb.Append("</ul>");
            }
        }
        private void AddNodes(DataTable tabs, string WFClassId, StringBuilder sb)
        {
            if (tabs.Select("WFClassId = '" + WFClassId + "'").Count() > 0)
            {
                sb.Append("<ul>");
                foreach (DataRow v in tabs.Select("WFClassId = '" + WFClassId + "'"))
                {
                    sb.Append("<li>");
                    sb.Append("<a href='#@" + v["WorkFlowId"].ToString() + "' id='%" + v["WorkFlowId"].ToString() + "'>" + v["FlowCaption"].ToString() + "</a>");
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();  //实例化流程服务接口 
            List<CrmWorkFlowService.AccreditUserEntity> AccreditUserList = new List<CrmWorkFlowService.AccreditUserEntity>();//代理实体集合

            string AccreditFromName = this.txtAccreditToName.Text;//被代理人名称
            string AccreditFromUserId = this.Page.User.Identity.Name;//被代理人ID
            string AccreditToName= this.txtCustomId.Value;//代理人名称
            string AccreditToUserId= CustomId.Value;//代理人ID

            DateTime AccreditDateTime = DateTime.Now;//创建时间
            char AccreditStatus = '1';//代理状态
            if (Start_Date.Value == "")
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('开始时间不能为空')</script>");
                return;
            }
            DateTime AcceditStartTime = Convert.ToDateTime(Start_Date.Value);//开始时间
            if (Report_Date.Value == "")
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('结束时间不能为空')</script>");
                return;
            }
            DateTime AcceditEndTime = Convert.ToDateTime(Report_Date.Value);//结束时间
            string AccreditName = this.txtAccreditToName.Text;//创建人
            char AccreditEstablish = '1'; //是否收回流转流程
          
            if (txtvlues.Text.ToString() != "")
            {
                 string[] str = txtvlues.Text.Split(',');//流程ID
                 if (str.Length > 0)
                 {
                     for (int i = 0; i < str.Length; i++)
                     {
                         CrmWorkFlowService.AccreditUserEntity AccreditUser = new CrmWorkFlowService.AccreditUserEntity(); //代理实体
                         AccreditUser.AccreditId = Guid.NewGuid().ToString();
                         AccreditUser.AccreditToName = AccreditToName;
                         AccreditUser.AccreditToUserId = AccreditToUserId;
                         AccreditUser.AccreditFromName = AccreditFromName;
                         AccreditUser.AccreditFromUserId = AccreditFromUserId;
                         AccreditUser.AccreditDateTime = AccreditDateTime;
                         AccreditUser.AccreditStatus = AccreditStatus;
                         AccreditUser.AcceditStartTime = AcceditStartTime;
                         AccreditUser.AcceditEndTime = AcceditEndTime;
                         AccreditUser.AccreditName = AccreditName;
                         AccreditUser.AcceditCancelTime = DateTime.Now;
                         AccreditUser.AccreditMatters = AccreditEstablish;
                         AccreditUser.WorkFlowId = str[i];
                         AccreditUserList.Add(AccreditUser);
                     }
                     string reust = workFlowServiceClient.AddAccreditUser(AccreditUserList);
                     if (reust =="1")
                     {
                         this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('保存成功')</script>");
                     }
                     else 
                     {
                         reust = "以下流程代理已存在-------------------" + reust;
                         this.ClientScript.RegisterStartupScript(this.GetType(), "Save", "<script>alert('" + reust + "')</script>");
                     }
                  }
            }
        }
    
    }
    
}